package com.multi.thread.chapter04.demo08;

import java.util.concurrent.locks.ReentrantLock;

/**
 *	getQueueLength() 返回正等待获取此锁定的线程估计数
 */
public class TestQueueLength {
	
	private ReentrantLock lock = new ReentrantLock();
	
	public void serviceMethod1() {
		try {
			lock.lock();
			System.out.println("ThreadName = " + Thread.currentThread().getName() + "进入方法了!");
			Thread.sleep(Integer.MAX_VALUE);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		final TestQueueLength service = new TestQueueLength();
		Runnable run = new Runnable() {
			public void run() {
				service.serviceMethod1();
			}
		};
		Thread[] thread = new Thread[10];
		for (int i = 0; i < 10; i++) {
			thread[i] = new Thread(run);
			thread[i].start();
		}
		Thread.sleep(200);
		System.out.println("有线程数：" + service.lock.getQueueLength() + "在等待获取锁!");
	}

}
